home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / objeng.arj / OBJENG.HPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  6.0 KB  |  185 lines

  1. /****************************************************************************
  2.  * Copyright (c) 1993, BSDI.  All rights reserved.
  3.  * Library Name...  Object Engine
  4.  * File Name......  objeng.hpp
  5.  * Comments.......  This file contains the class definitions and #defines
  6.  ***************************************************************************/
  7. #ifndef _OEDBH_
  8. #define _OEDBH_
  9.  
  10. #include <string.h>
  11. #include <time.h>
  12. #include <pxengine.h>
  13.  
  14. #ifndef TRUE
  15. #define TRUE 1
  16. #define FALSE 0
  17. #endif
  18.  
  19. #ifndef NULL
  20. #define NULL 0
  21. #endif
  22.  
  23. const int MAXFIELDS=64;
  24. const int OE_MAXTABLES=32;
  25. const int NOTINIT = 0;
  26. const int FAIL = 1;
  27. const int READY = 2;
  28.  
  29. class OEList;
  30.  
  31. //-----------------------------------------------------------
  32. typedef struct {
  33.   char TableName[12];
  34.   TABLEHANDLE tHdl;
  35.   RECORDHANDLE rHdl;
  36. } OEHandle;
  37.  
  38.  
  39. //-----------------------------------------------------------
  40. class ObjEng {
  41. private:
  42.   int status;
  43.   int OESaveChanges;
  44.   char filedir[64];
  45.   OEHandle RegisteredTables[OE_MAXTABLES];
  46.   TABLEHANDLE tHdl;
  47.   RECORDHANDLE rHdl;
  48.   int TableInit();
  49. public:
  50. #ifdef _Windows
  51.   ObjEng(char *clientName, int shareMode, int bufsize, int maxContainer, int saveChanges, char *fd);
  52.   ObjEng(char *clientName);  //Uses all defaults.
  53. #else
  54.   ObjEng(int bufsize, int maxContainer, int saveChanges, char *fd);
  55.   ObjEng();  //Uses all defaults.
  56. #endif
  57.   ~ObjEng();
  58.   int GetIndex(long &index, int listFlag);
  59.   int Register(OEHandle &oehandle, char **FieldNames, char **FieldTypes, FIELDHANDLE *IdxFieldHandles, int nFields, int nIdxFields);
  60.   int IsRegistered(OEHandle &oehandle);
  61.   int AttachChild(long &parent, const char *tableName, const long &objectID);
  62.   int DetachChild(const long &parent, const long &child);
  63.   int GetChild(long &parent, char *tbl, long &child, int mode);
  64.   const char *GetFileDir() { return filedir; }
  65.   const int GetStatus() { return status; }
  66. };
  67.  
  68.  
  69. //-------------------------------------------------------------------------
  70. class PersistClass {
  71. protected:
  72.   friend OEList;
  73.   PersistClass *next;
  74.  
  75.   // Holds the name of and Paradox Engine handles for the table that
  76.   // matches this persistant class.
  77.   OEHandle oehandle;
  78.  
  79.   // The objectID is at the center of the persistance system.
  80.   // It is the "link" between the object and a db table record.
  81.   // It is assigned when a virgin object is Stored and cleared when 
  82.   // Destroyed.  Each of the link functions resets objectID to that 
  83.   // of the db record being retrieved when the client object is filled from 
  84.   // the database.  The copy constructor and assignment operator do NOT
  85.   // copy it since that would mix up two client objects - contrary to the
  86.   // philosophy of the system.
  87.   long objectID;
  88.  
  89.         // "C" Types - used during the "LinkToField" function.
  90.   char localtypes[MAXFIELDS]; 
  91.  
  92.   // Status holds the most recent Engine return code.  Most functions
  93.   // return this value for user to test against.  Uses engine error
  94.   // codes exclusively!
  95.   int status;
  96.  
  97.   // DeleteFlag tells a container whether this object should be deleted
  98.   // when the container is destroyed or during a call to OEList::Remove.
  99.   // By default, it is 0 UNLESS the object was automatically created
  100.   // during a load from the database.  You can set this if you want the
  101.   // object engine to delete objects in a list for you (see below).
  102.   char deleteFlag;
  103.  
  104.   // Retrieve is defined by derived classes to indicate how the linking
  105.   // required by the functions below is actually implemented.  It 
  106.   // must be in the derived classes since they are the ones to know the
  107.   // structure of the object/db record.
  108.   virtual int Retrieve() = 0;
  109.   // Utility to copy PersistClass data without the objectID link.
  110.   void Copy(PersistClass &pc);
  111. public:
  112.   // Default class constructor.
  113.   PersistClass(char *name);
  114.   // Used in the copy constructor.
  115.   PersistClass() {}
  116.  
  117.   // Store places the contents of an object into the data table.  If
  118.   // the object is not linked (no objectID) then an objectID is assigned.
  119.   // Otherwise, the contents of the object are placed in the record
  120.   // corresponding to the objectID.
  121.   virtual int Store() = 0;
  122.  
  123.   // Table/C++ Class "Linking" functions - allow class instances to
  124.   // be associated with DB records.
  125.   virtual int LinkToKey(int, int);
  126.   int LinkToRecord(int position);
  127.   int LinkToField(char *fieldname, void *value, int mode);
  128.   int LinkToID(const long &ID);
  129.   int LinkToFirst();
  130.   int LinkToLast();
  131.   int LinkToNext();
  132.   int LinkToPrev();
  133.  
  134.   // "Unlinking" functions.  Also, the copy constructor and assignment 
  135.   // operator will unlink the recipient of the copy/assignment.
  136.   void Unlink();
  137.   virtual int Destroy();
  138.  
  139.   // Utilities
  140.   int NRecs(RECORDNUMBER & nrecs);
  141.   int OEPutDate(const int &, const struct tm &);
  142.   int OEGetDate(const int &, struct tm &);
  143.   const int GetStatus() { return status; }
  144.   TABLEHANDLE GetTblHdl() { return oehandle.tHdl; }
  145.   RECORDHANDLE GetRecHdl() { return oehandle.rHdl; }
  146.   const char *GetTblName() { return oehandle.TableName; }
  147.   long GetObjectID() { return objectID; }
  148.   char GetDeleteFlag() { return deleteFlag; }
  149.   char SetDeleteFlag(char flag) { return(deleteFlag = flag); }
  150. };
  151.  
  152.  
  153. //----------------------------------------------------------------
  154. class OEList {
  155. private:
  156.   long listID;
  157.   int status;
  158.   int referenceCount;
  159.   PersistClass *head, *cur;
  160. public:
  161.   OEList();
  162.   OEList(long inID);
  163.   int Init();
  164.   ~OEList();
  165.   void Insert(PersistClass *curelem);
  166.   void Remove();
  167.   PersistClass *operator()();
  168.   PersistClass *operator++(int);
  169.   void Reset() { cur = head; }
  170.   int Count();
  171.   void Clear();
  172.   void AddReference() { referenceCount++; }
  173.   char DeleteOK() { referenceCount--; return(referenceCount == 1); }
  174.   const int GetStatus() { return status; }
  175.   const long GetListID() { return listID; }
  176. };
  177.  
  178.  
  179. // This "global" function provides the mapping from table names
  180. // to table objects for the automatic "has-a" link storage/retrieval.
  181. PersistClass *GetPersistClass(char *tbl, const long &inID);
  182.  
  183.  
  184. #endif
  185.